package com.tencreat.mall.controller.app;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.json.JSONArray;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.dto.AfterSaleDTO;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.enums.AfterSaleStatus;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.TenCreatUtil;
import com.tencreat.domain.mall.MallAftersale;
import com.tencreat.domain.mall.MallOrderGoods;
import com.tencreat.domain.mall.dto.AftersaleAddDTO;
import com.tencreat.domain.mall.dto.CancelAftersaleDTO;
import com.tencreat.domain.mall.dto.MallAftersaleDTO;
import com.tencreat.domain.mall.vo.MallAftersaleVO;
import com.tencreat.service.mall.IMallAftersaleService;
import com.tencreat.service.mall.IMallOrderGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * 售后Controller
 *
 * @author tencreat
 * @date 2021-08-15
 */
@Api(tags="90-售后api")
@RestController
@RequestMapping("/api/aftersale")
public class MallAftersaleController extends BaseController {

    @Autowired
    private IMallAftersaleService mallAftersaleService;
    @Autowired
    private IMallOrderGoodsService mallOrderGoodsService;


    /**
     * 修改退货物流
     */
    @ApiOperation(value="修改退货物流")
    @Log(title = "修改退货物流", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateShip")
    public HttpResult<Boolean> updateShip(@RequestBody @Validated MallAftersaleDTO mallAftersaleDTO) {
        MallAftersale aftersale = mallAftersaleService.lambdaQuery().eq(MallAftersale::getOrderGoodsId, mallAftersaleDTO.getOrderGoodsId()).one();
        aftersale.setShipSn(mallAftersaleDTO.getShipSn());
        aftersale.setStatus(AfterSaleStatus.WAIT_RECEIVED.getValue());
        aftersale.setShipName(mallAftersaleDTO.getShipName());
        aftersale.setEndTime(LocalDateTimeUtil.offset(LocalDateTime.now(),7, ChronoUnit.DAYS));
        boolean update = mallAftersaleService.updateById(aftersale);
        if (update){
            // 修改订单售后状态
            mallOrderGoodsService.lambdaUpdate().eq(MallOrderGoods::getId,mallAftersaleDTO.getOrderGoodsId())
                    .set(MallOrderGoods::getAftersaleStatus,AfterSaleStatus.WAIT_RECEIVED)
                    .update();
            // 超过三天自动收货
            AfterSaleDTO dto = new AfterSaleDTO();
            dto.setStatus(AfterSaleStatus.DEL_PAY.getValue());
            // 标记为退货退款中
            dto.setType(1);
            dto.setOrderGoodsId(aftersale.getOrderGoodsId());
            dto.setOrderId(aftersale.getOrderId());
            dto.setId(aftersale.getId());
            mallAftersaleService.sendDelayMessageAfterSale(dto,7);
        }

        return to(update);
    }

    /**
     * 查询售后信息
     */
    @ApiOperation(value="查询售后信息")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallAftersale> getInfo(@RequestBody @Validated ReqIdParam id){
        return HttpResult.ok(mallAftersaleService.lambdaQuery().eq(MallAftersale::getOrderGoodsId, id.getId()).one());
    }

    /**
     * 新增售后  针对于商品 而非订单号
     */
    @ApiOperation(value="新增售后")
    @Log(title = "售后", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody AftersaleAddDTO mallAftersale) {
        // 售后信息
        mallAftersale.setType(0);
        mallAftersale.setUserId(getLoginCheck().getAccountLogin().getUuid());
        mallAftersale.setMobile(getLoginCheck().getAccountLogin().getPhone());
        mallAftersale.setPictures( mallAftersale.getPictures());
        mallAftersale.setAftersaleSn(TenCreatUtil.getOrderNo());
        mallAftersale.setStatus(3);
        MallAftersale mallAftersale1 = new MallAftersale();
        BeanUtils.copyProperties(mallAftersale,mallAftersale1);

        return to(mallAftersaleService.insertMallAftersale(mallAftersale1));
    }

    @ApiOperation(value="取消售后（取消退款）")
    @Log(title = "取消售后", businessType = BusinessType.INSERT)
    @PostMapping(value = "/cancelAfterSale")
    public HttpResult<Boolean> cancelAfterSale(@RequestBody CancelAftersaleDTO dto) {
        return to(mallAftersaleService.cancelAfterSale(dto));
    }
}
